package com.sojson.controller.po;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.MessageSystemParamVerify;
import com.sojson.entity.po.MessageSystem;
import com.sojson.result.Result;
import com.sojson.service.MessageSystemService;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 系统消息表控制器
 * 
 * @author liu
 * @date 2021-03-22
 */
@Api(tags = "系统消息表管理")
@RestController
@RequestMapping(value = "/messageSystem")
public class MessageSystemController {

    @Resource
    private MessageSystemService messageSystemServiceImpl;

    /**
     * 单条插入
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Object insert(@RequestBody MessageSystem messageSystem) throws SQLException {
        messageSystemServiceImpl.insert(messageSystem);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody MessageSystem messageSystem) throws SQLException {
        return Result.success(messageSystemServiceImpl.insertList(messageSystem));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody MessageSystem messageSystem) throws SQLException {
        return Result.success(messageSystemServiceImpl.deleteByDto(messageSystem));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = messageSystemServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<MessageSystem> findAll = messageSystemServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countByDto")
    public Object countByDto(MessageSystem messageSystem) throws SQLException {
        int countByDto = messageSystemServiceImpl.countByDto(messageSystem);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByDto")
    public Object findByDto(MessageSystem messageSystem) throws SQLException {
        List<MessageSystem> findByDto = messageSystemServiceImpl.findByDto(messageSystem);
        return Result.success(findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Object deleteById(Integer id) throws SQLException {
        messageSystemServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/deleteByIdList")
    @PreAuthorize("@ss.login()")
    public Object deleteByIdList(@RequestParam("ids") ArrayList<Integer> ids) throws SQLException {
        if (ids.size() < 1) {
            return Result.success();
        }
        return Result.success(messageSystemServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody MessageSystem messageSystem) throws SQLException {
        messageSystemServiceImpl.updateById(messageSystem);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody MessageSystem messageSystem) throws SQLException {
        return Result.success(messageSystemServiceImpl.updateStaticByIdList(messageSystem));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody MessageSystem messageSystem) throws SQLException {
        return Result.success(messageSystemServiceImpl.updateChangeByIdList(messageSystem));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updatePartialById", method = RequestMethod.POST)
    public Object updatePartialById(@RequestBody MessageSystem messageSystem) throws SQLException {
        messageSystemServiceImpl.updatePartialById(messageSystem);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody MessageSystem messageSystem) throws SQLException {
        return Result.success(messageSystemServiceImpl.updateStaticPartialByIdList(messageSystem));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody MessageSystem messageSystem) throws SQLException {
        return Result.success(messageSystemServiceImpl.updateChangePartialByIdList(messageSystem));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws IOException 
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @PreAuthorize("@ss.login()")
    public Object findById(Integer id) throws IOException {
        MessageSystem findById = messageSystemServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<MessageSystem> findByIdList = messageSystemServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody MessageSystem messageSystem) throws SQLException {
        ArrayList<Integer> findIdByDto = messageSystemServiceImpl.findIdByDto(messageSystem);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        messageSystemServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/findXcxByDto")
    @PreAuthorize("@ss.login()")
    public Object findXcxByDto(MessageSystem messageSystem) throws SQLException, IOException {
        messageSystem.setMid(TokenUtil.getUserId());
        List<MessageSystem> findByDto = messageSystemServiceImpl.findByDto(messageSystem);
        return Result.success(findByDto);
    }

    /**
     * 根据对象统计数量
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/countXcxByDto")
    @PreAuthorize("@ss.login()")
    public Object countXcxByDto(MessageSystem messageSystem) throws SQLException, IOException {
        messageSystem.setMid(TokenUtil.getUserId());
        int countByDto = messageSystemServiceImpl.countByDto(messageSystem);
        return Result.success(countByDto);
    }

    /**
     * 修改读取状态
     * 
     * @param messageSystem
     * @return
     * @throws SQLException
     */
    @PutMapping("/updateIsRead")
    @PreAuthorize("@ss.login()")
    public Object updateIsRead(@RequestBody MessageSystem messageSystem) throws SQLException {
        // 校验参数
        Result<?> paramResult = MessageSystemParamVerify.updateIsRead(messageSystem);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        MessageSystem msg = new MessageSystem();
        msg.updateInit();
        msg.setId(messageSystem.getId());
        msg.setIsRead(messageSystem.getIsRead());
        messageSystemServiceImpl.updatePartialById(msg);
        return Result.success();
    }

}